<template>
    <div class="app-container">
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="onClickAdd"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
  
      <!-- 表格   -->
      <el-table
        v-loading="loading"
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="描述" align="center" prop="content" />
        <el-table-column label="重复周期" align="center" prop="voiceName" />
        <el-table-column
          label="呼叫时段"
          align="center"
          prop="speedRatio"
        ></el-table-column>
        <el-table-column label="创建时间" align="center" prop="volumeRatio" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              v-if="scope.row.ttsUrl"
              @click="onAudioDownload(scope.row)"
              type="text"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="pagination.pageNum"
        :limit.sync="pagination.pageSize"
        @pagination="getList"
      />

      <add-timed-plan-dialog :addTimedPlanDialogShow="addTimedPlanDialogShow"></add-timed-plan-dialog>
    </div>
  </template>
  ``
  <script>
  import { getList } from "@/api/customerService/tts-test-list";
  import { listData } from "@/api/system/dict/data";
import addTimedPlanDialog from './components/addTimedPlanDialog.vue';
  export default {
  components: { addTimedPlanDialog },
    name: "ttsTestList",
    data() {
      return {
        loading: false,
        tableData: [], //表格数据
        showSearch: true, //显示搜索条件
        // 查询参数
        formData: {
          chatText: "", //测试文本
          voiceType: "", //音色
          pitchRatio: "", //音高
          speedRatio: "", //语速
        },
        voiceTypeList: [], //音色数据
        pagination: {
          pageNum: 1,
          pageSize: 10,
        },
        dateRange: [], //日期范围
        total: 0,
        importShow: false, //导入
        addTimedPlanDialogShow: false,
        voiceTypeList: [],
      };
    },
    async created() {
      await this.getListType();
      await this.getList();
    },
    methods: {
      async getListType() {
        const { rows } = await listData({
          pageNum: 1,
          pageSize: 1000,
          dictType: "sys_voice_type",
        });
        this.voiceTypeList = rows || [];
      },
      // 列表查询
      async getList() {
        const voiceAll = this.voiceTypeList.find(
          (el) => el.dictValue == this.formData.voiceType
        );
        const formData = JSON.parse(JSON.stringify(this.formData));
        delete formData["voiceType"];
        let params = {
          ...formData,
          voiceName: voiceAll?.dictLabel || undefined, //音色
          ...this.pagination,
        };
        this.loading = true;
        const { total, rows } = await getList(
          this.addDateRange(params, this.dateRange)
        );
        this.total = total;
        this.tableData = rows;
        this.loading = false;
      },
  
      /** 搜索按钮操作 */
      handleQuery() {
        this.pagination.pageNum = 1;
        this.getList();
      },
  
      /** 重置按钮操作 */
      resetQuery() {
        this.formData = {
          chatText: "", //测试文本
          voiceType: "", //音色
          pitchRatio: "", //音高
          speedRatio: "", //语速
        };
        this.dateRange = [];
        this.handleQuery();
      },
  
      /** 导出按钮操作 */
      handleExport() {
        const url = `system/sysTtsVoice/exportTtsVoice`;
        const voiceAll = this.voiceTypeList.find(
          (el) => el.dictValue == this.formData.voiceType
        );
        const formData = JSON.parse(JSON.stringify(this.formData));
        delete formData["voiceType"];
        let data = {
          ...formData,
          voiceName: voiceAll?.dictLabel || undefined, //音色
          ...this.pagination,
        };
        if (this.dateRange?.length) {
          data.params =
            typeof data.params === "object" &&
            data.params !== null &&
            !Array.isArray(data.params)
              ? data.params
              : {};
          data.params["beginTime"] = this.dateRange[0];
          data.params["endTime"] = this.dateRange[1];
        }
        this.download(
          url,
          {
            ...data,
          },
          `ttsTestList_${new Date().getTime()}.xlsx`
        );
      },
  
      // 音频下载
      async onAudioDownload(row) {
        console.log(row, "row");
        const fileUrl = row?.ttsUrl;
        // 打开新标签页进行下载
        window.open(fileUrl, "_blank");
      },
  
      // 新建
      onClickAdd() {
        this.addTimedPlanDialogShow = true;
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.userId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
  
      // 取消
      onClose() {
        this.addTimedPlanDialogShow = false;
        this.getList();
      },
    },
  };
  </script>
  
  <style lang="scss" scoped></style>
  